using System;
using System.Windows.Forms;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;


namespace OhioState.Controls
{
    //
    // Material Color Properties Dialog - Popup dialog to modify material color
    //
    public partial class MaterialColorDialog : Form
    {
        //private IAssetManagedListHandler selectEvent;
        private MaterialProxy material;
        private MaterialColors materialColors;
        private bool _front;

        public MaterialColorDialog()
        {
            material = null;
            materialColors = null;
            InitializeComponent();
        }

        //
        // Populate dialog based on the color object given.  Use the True Color
        //  not the color with alpha values for the color dialog and the panel.
        //
        public void ShowAndInit(MaterialProxy mat, bool front)
        {
            _front = front;
            if (_front)
            {
                materialColors = mat.FrontMaterial;
                interactiveAlphapicker1.init(mat, materialColors.AmbientColor.A, ColorKind.FRONT_AMBIENT);
                interactiveAlphapicker2.init(mat, materialColors.DiffuseColor.A, ColorKind.FRONT_DIFFUSE);
                interactiveAlphapicker3.init(mat, materialColors.SpecularColor.A, ColorKind.FRONT_SPECULAR);
                interactiveAlphapicker4.init(mat, materialColors.EmissiveColor.A, ColorKind.FRONT_EMISSIVE);
            }
            else
            {
                materialColors = mat.BackMaterial;
                interactiveAlphapicker1.init(mat, materialColors.AmbientColor.A, ColorKind.BACK_AMBIENT);
                interactiveAlphapicker2.init(mat, materialColors.DiffuseColor.A, ColorKind.BACK_DIFFUSE);
                interactiveAlphapicker3.init(mat, materialColors.SpecularColor.A, ColorKind.BACK_SPECULAR);
                interactiveAlphapicker4.init(mat, materialColors.EmissiveColor.A, ColorKind.BACK_EMISSIVE);
            }

            colorPanel1.BackColor = materialColors.AmbientColor.TrueColor;
            colorPanel2.BackColor = materialColors.DiffuseColor.TrueColor;
            colorPanel3.BackColor = materialColors.SpecularColor.TrueColor;
            colorPanel4.BackColor = materialColors.EmissiveColor.TrueColor;
            colorDialog1.Color = materialColors.AmbientColor.TrueColor;
            colorDialog2.Color = materialColors.DiffuseColor.TrueColor;
            colorDialog3.Color = materialColors.SpecularColor.TrueColor;
            colorDialog4.Color = materialColors.EmissiveColor.TrueColor;
            specularExpUpDown.Value = (decimal)(materialColors.SpecularExponent);
            material = mat;
            //DialogResult result = this.ShowDialog();
            //if (result == DialogResult.OK || result == DialogResult.Cancel)
            //{
            //    material.BeginManipulation();
            //    if (_front)
            //    {
            //        material.FrontMaterial.AmbientColor.A = (float)interactiveAlphapicker1.Value / 100.0f;
            //        material.FrontMaterial.DiffuseColor.A = (float)interactiveAlphapicker2.Value / 100.0f;
            //        material.FrontMaterial.SpecularColor.A = (float)interactiveAlphapicker3.Value / 100.0f;
            //        material.FrontMaterial.EmissiveColor.A = (float)interactiveAlphapicker4.Value / 100.0f;
            //    }
            //    else
            //    {
            //        material.BackMaterial.AmbientColor.A = (float)interactiveAlphapicker1.Value / 100.0f;
            //        material.BackMaterial.DiffuseColor.A = (float)interactiveAlphapicker2.Value / 100.0f;
            //        material.BackMaterial.SpecularColor.A = (float)interactiveAlphapicker3.Value / 100.0f;
            //        material.BackMaterial.EmissiveColor.A = (float)interactiveAlphapicker4.Value / 100.0f;
            //    }
            //    material.EndManipulation();
            //}
            this.Show();
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void colorPanel1_DoubleClick(object sender, EventArgs e)
        {
            if (material != null)
            {
                colorDialog1.Color = materialColors.AmbientColor.TrueColor;
                if (colorDialog1.ShowDialog() == DialogResult.OK)
                {
                    //
                    // Update ambient color with the color from the dialog but preserve the alpha value
                    //
                    float alpha = materialColors.AmbientColor.A;
                    materialColors.AmbientColor.Color = colorDialog1.Color;
                    materialColors.AmbientColor.A = alpha;
                    material.BeginManipulation();
                    if (_front)
                        material.FrontMaterial.AmbientColor = materialColors.AmbientColor;
                    else
                        material.BackMaterial.AmbientColor = materialColors.AmbientColor;
                    material.EndManipulation();
                    colorPanel1.BackColor = materialColors.AmbientColor.TrueColor;
                }
            }
        }

        private void colorPanel2_DoubleClick(object sender, EventArgs e)
        {
            if (material != null)
            {
                colorDialog2.Color = materialColors.DiffuseColor.TrueColor;
                if (colorDialog2.ShowDialog() == DialogResult.OK)
                {
                    //
                    // Update ambient color with the color from the dialog but preserve the alpha value
                    //
                    float alpha = materialColors.DiffuseColor.A;
                    materialColors.DiffuseColor.Color = colorDialog2.Color;
                    materialColors.DiffuseColor.A = alpha;
                    material.BeginManipulation();
                    if (_front)
                        material.FrontMaterial.DiffuseColor = materialColors.DiffuseColor;
                    else
                        material.BackMaterial.DiffuseColor = materialColors.DiffuseColor;
                    material.EndManipulation();
                    colorPanel2.BackColor = materialColors.DiffuseColor.TrueColor;
                }
            }
        }

        private void colorPanel3_DoubleClick(object sender, EventArgs e)
        {
            if (material != null)
            {
                colorDialog3.Color = materialColors.SpecularColor.TrueColor;
                if (colorDialog3.ShowDialog() == DialogResult.OK)
                {
                    //
                    // Update ambient color with the color from the dialog but preserve the alpha value
                    //
                    float alpha = materialColors.SpecularColor.A;
                    materialColors.SpecularColor.Color = colorDialog3.Color;
                    materialColors.SpecularColor.A = alpha;
                    material.BeginManipulation();
                    if (_front)
                        material.FrontMaterial.SpecularColor = materialColors.SpecularColor;
                    else
                        material.BackMaterial.SpecularColor = materialColors.SpecularColor;
                    material.EndManipulation();
                    colorPanel3.BackColor = materialColors.SpecularColor.TrueColor;
                }
            }
        }

        private void colorPanel4_DoubleClick(object sender, EventArgs e)
        {
            if (material != null)
            {
                colorDialog4.Color = materialColors.EmissiveColor.TrueColor;
                if (colorDialog4.ShowDialog() == DialogResult.OK)
                {
                    //
                    // Update ambient color with the color from the dialog but preserve the alpha value
                    //
                    float alpha = materialColors.EmissiveColor.A;
                    materialColors.EmissiveColor.Color = colorDialog4.Color;
                    materialColors.EmissiveColor.A = alpha;
                    material.BeginManipulation();
                    if (_front)
                        material.FrontMaterial = materialColors;
                    else
                        material.BackMaterial = materialColors;
                    material.EndManipulation();
                    colorPanel4.BackColor = materialColors.EmissiveColor.TrueColor;
                }
            }
        }

        private void specularExpUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (material != null)
            {
                materialColors.SpecularExponent = Decimal.ToInt32(specularExpUpDown.Value);
                material.BeginManipulation();
                if (_front)
                    material.FrontMaterial.SpecularExponent = materialColors.SpecularExponent;
                else
                    material.BackMaterial.SpecularExponent = materialColors.SpecularExponent;
                material.EndManipulation();
            }
        }
    }
}